package com.wzu.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wzu.entity.Division;
import com.wzu.entity.Result;
import com.wzu.service.DivisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/division")
@CrossOrigin
public class DivisionController {
    @Autowired
    private DivisionService divisionService;

    // 查询所有区域，支持按级别和父级ID过滤
    @GetMapping("/list")
    public Result getDivisions(@RequestParam(required = false) Integer level,
                               @RequestParam(required = false) String parentId) {
        return new Result("查询成功", divisionService.getDivisions(level, parentId), 200);
    }

    // 按名称模糊查询
    @GetMapping("/search")
    public Result searchDivisions(@RequestParam(required = false) String name) {
        return new Result("查询成功", divisionService.getDivisionsByName(name), 200);
    }

    // 添加区域
    @PostMapping("/add")
    public Result addDivision(@RequestBody Division division) {
        if (divisionService.addDivision(division)) {
            return new Result("添加成功", null, 200);
        } else {
            return new Result("区域编码已存在", null, 400);
        }
    }

    // 更新区域
    @PutMapping("/update")
    public Result updateDivision(@RequestBody Division division) {
        if (divisionService.updateDivision(division)) {
            return new Result("更新成功", null, 200);
        } else {
            return new Result("区域编码已存在或更新失败", null, 400);
        }
    }

    // 删除区域
    @DeleteMapping("/delete/{id}")
    public Result deleteDivision(@PathVariable Long id) {
        if (divisionService.deleteDivision(id)) {
            return new Result("删除成功", null, 200);
        } else {
            return new Result("删除失败", null, 400);
        }
    }
} 